<?php
namespace app\common\model;
use think\Db;
class Data{
    /**
     * 根据k值取得数据
     * @param string/array $k 搜索的K值，可以是单个，也可以传数组搜索多条数据
     * @return array
     */
    static public function getSys( $k = [] ){
        if(is_array($k)){
            if(empty($k)){
                $rs = Db::name('sys')->select();
            }else{
                $map['k'] = ['in',$k];
                $rs = Db::name('sys')->where($map)->select();
            }
            if(!$rs)
                return com_error(['没有找到系统配置']);
    
            //格式化图片
            $temp = [];
            foreach($rs as $key => $val){
                if( $val['type'] == 'pic' && !empty($val['v']) ){
                    $temp[$val['k']] = com_get_pic($val['v']);
                }else{
                    $temp[$val['k']] = $val['v'];
                }
            }
            return $temp;
    
        }elseif( is_string($k) ){
            $rs = Db::name('sys')->where('k', $k)->find();
            if(!$rs)
                return com_error(['没有找到系统配置']);
            //格式化图片
            if( $rs['type'] == 'pic' && !empty($rs['v']) )
                $rs['v'] = com_get_pic($rs['v']);
            
            return $rs['v'];
        }
    }
    /**
     * 获取网页碎片
     * @param unknown $uuid 碎片分类的uuid
     */
    static public function getExt( $uuid = [] ){
        if(is_array($uuid)){
            if(empty($uuid)){
                $sql = "select a.*,b.uuid as buuid from ext a left join ext_cat b on a.cat_id=b.id where true order by sort desc,id desc";
                $rs  = Db::query($sql);
            }else{
                $uuids = implode(",", $uuid);
                $sql = "select a.*,b.uuid as buuid from ext a left join ext_cat b on a.cat_id=b.id where uuid in ('{$uuids}') order by sort desc,id desc";
                $rs  = Db::query($sql);
            }
            if(!$rs)
                return com_error(['没有找到系统配置']);
            
            $temp = [];
            foreach($rs as $key => $val){
                $temp[$val['buuid']][$val['uuid']] = $val['type']==1 ? com_get_pic($val['content']) : $val['content'];
            }
            return $temp;
    
        }elseif( is_string($uuid) ){
            $sql = "select a.* from ext a left join ext_cat b on a.cat_id=b.id where b.uuid=? order by sort desc,id desc";
            $rs = Db::query($sql,[$uuid]);
            if(!$rs)
                return com_error(['没有找到系统配置']);
            $temp = [];
            foreach($rs as $v){
                $temp[$v['uuid']] = $v['type']==1 ? com_get_pic($v['content']) : $v['content'];
            }
            return $temp;
        }
    }
}